package com.health.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.common.PageBean;
import com.health.dto.CheckitemQueryDTO;
import com.health.exception.CheckitemCodeExistedException;
import com.health.exception.CheckitemNameExistedException;
import com.health.exception.DeletionNotAllowedException;
import com.health.exception.ParameterInvalidException;
import com.health.mapper.CheckgroupCheckitemMapper;
import com.health.mapper.CheckitemMapper;
import com.health.pojo.Checkitem;
import com.health.service.CheckitemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 检查项业务实现
 *
 * @author zgp
 * @version V1.0
 * @date 2025-01-18 19:02
 */
@Service
@RequiredArgsConstructor
public class CheckitemServiceImpl implements CheckitemService {

    private final CheckitemMapper checkitemMapper;
    private final CheckgroupCheckitemMapper checkgroupCheckitemMapper;


    @Override
    public PageBean<Checkitem> findPageList(CheckitemQueryDTO checkitemQueryDTO) {
        PageHelper.startPage(checkitemQueryDTO.getCurrentPage(), checkitemQueryDTO.getPageSize());
        Page<Checkitem> checkitemPage = checkitemMapper.selectPageList(checkitemQueryDTO.getQueryString());
        return new PageBean<>(checkitemPage.getTotal(), checkitemPage.getResult());
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void addCheckitem(Checkitem checkitem) {
        // 参数校验：项目编码、项目名称、类型、价格：不为空校验
        if (StrUtil.isEmpty(checkitem.getCode())
                || StrUtil.isEmpty(checkitem.getName())
                || StrUtil.isEmpty(checkitem.getType())
                || Objects.isNull(checkitem.getPrice())) {
            throw new ParameterInvalidException("新增检查项参数非法");
        }
        // 项目编码重复校验
        Checkitem item = checkitemMapper.selectByCode(checkitem.getCode());
        if (Objects.nonNull(item)) {
            throw new CheckitemCodeExistedException("检查项编码已存在");
        }
        // 项目名称重复校验
        item = checkitemMapper.selectByName(checkitem.getName());
        if (Objects.nonNull(item)) {
            throw new CheckitemNameExistedException("检查项名称已存在");
        }
        // 属性补充
        if (StrUtil.isEmpty(checkitem.getSex())) {
            checkitem.setSex("0");
        }
        if (StrUtil.isEmpty(checkitem.getAge())) {
            checkitem.setAge("0-100");
        }
        checkitemMapper.insert(checkitem);
    }

    @Override
    public Checkitem findById(Integer id) {
        if (Objects.isNull(id)) {
            throw new ParameterInvalidException("ID不能为空");
        }
        return checkitemMapper.selectById(id);
    }

    @Override
    public void deleteCheckitemById(Integer id) {
        if (Objects.isNull(id)) {
            throw new ParameterInvalidException("ID不能为空");
        }
        // 如果检查组中存在该检查项，则不允许删除
        int count = checkgroupCheckitemMapper.countByCheckitemId(id);
        if(count > 0){
            throw new DeletionNotAllowedException("检查组中存在该检查项，不允许删除");
        }
        checkitemMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateCheckitemById(Checkitem checkitem) {
        // 参数校验：项目编码、项目名称、类型、价格：不为空校验
        if (StrUtil.isEmpty(checkitem.getCode())
                || StrUtil.isEmpty(checkitem.getName())
                || StrUtil.isEmpty(checkitem.getType())
                || Objects.isNull(checkitem.getPrice())) {
            throw new ParameterInvalidException("新增检查项参数非法");
        }
        Checkitem item = checkitemMapper.selectById(checkitem.getId());
        if (Objects.isNull(item)) {
            throw new ParameterInvalidException("修改的检查项不存在");
        }
        // 校验项目编码是否重复：先判断当前id下code是否被修改，有修改再判断code是否重复
        if (!item.getCode().equals(checkitem.getCode())) {
            // code有修改
            Checkitem existItemByCode = checkitemMapper.selectByCode(checkitem.getCode());
            if (Objects.nonNull(existItemByCode) && !existItemByCode.getId().equals(checkitem.getId())) {
                throw new CheckitemCodeExistedException("检查项编码已存在");
            }
        }
        // 校验项目名称是否重复
        if (!item.getName().equals(checkitem.getName())) {
            // name有修改
            Checkitem existItemByName = checkitemMapper.selectByName(checkitem.getName());
            if(Objects.nonNull(existItemByName) && !existItemByName.getId().equals(checkitem.getId())){
                throw new CheckitemNameExistedException("检查项名称已存在");
            }
        }
        // 修改
        checkitemMapper.updateById(checkitem);
    }

    @Override
    public List<Checkitem> findAll() {
        return checkitemMapper.selectAll();
    }
}
